<?php

/*
 * Universal object identifier
 * Consists of object class and its id, separated by colons 
 */
function uoi() {
  $params = func_get_args(); 
  return call_user_func_array(array('nixpObjectHelper', 'createUoi'), $params);
}

function display_none($condition = true, $return = false) {
  $style = $condition? 'display: none' : '';
  if ($return) {
    return $style;
  } else {
    echo $style;
  } 
}

function russian_ending($num, $endings = null) {
  if ($endings == null) {
    $endings = array(
      '',                 // 1
      'а',               // 234
      'ов',             // default
    );
  }
  if ($num>9 && $num<20) {
    return $endings[2];
  }
  $num = (string)$num;
  $lastDigit = $num[strlen($num)-1];
  $preLastDigit = isset($num[strlen($num)-2])? $num[strlen($num)-2] : null;
  switch ($lastDigit) {
    case 1:
      if ($preLastDigit == 1) {
        $ending = $endings[2];
      } else {
        $ending = $endings[0];
      }
      break;
    case 2:
    case 3:
    case 4:
      $ending = $endings[1];
      break;
    default:
      $ending = $endings[2];
      break;
  }
  return $ending;
}

function sex($user, $endings = null) {
  if (empty($endings)) {
    $endings = array('', 'а');
  }
  $sex = $user->getSex();
  return $sex? $endings[0] : $endings[1];
}

function extract_regex($validator) {
  if ($validator instanceof sfValidatorAnd || $validator instanceof sfValidatorOr) {
    $validators = $validator->getValidators();
    foreach ($validators as $childValidator) {
      $regex = extract_regex($childValidator);
      if ($regex) return $regex;
    }
  } else if ($validator instanceof sfValidatorRegex) {
    return $validator->getOption('pattern');
  }
}

function month($num) {
  $months = array(
    1 => 'Январь',
    2 => 'Февраль',
    3 => 'Март',
    4 => 'Апрель',
    5 => 'Май',
    6 => 'Июнь',
    7 => 'Июль',
    8 => 'Август',
    9 => 'Сентябрь',
    10 => 'Октябрь',
    11 => 'Ноябрь',
    12 => 'Декабрь'
  );
  return $months[$num];
}